package com.itheima.mobilesafe69.activity;

import com.itheima.mobilesafe69.R;
import com.itheima.mobilesafe69.db.dao.AddressDao;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class QueryAddresssActivity extends Activity {
	private static final String tag = "QueryAddresssActivity";
	private EditText et_phone;
	private Button bt_query;
	private TextView tv_result;
	private String mQueryAddress;
	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			tv_result.setText(mQueryAddress);
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_query_address);
		
		initUI();
	}

	private void initUI() {
		et_phone = (EditText) findViewById(R.id.et_phone);
		bt_query = (Button) findViewById(R.id.bt_query);
		tv_result = (TextView) findViewById(R.id.tv_result);
		
		//在点击按钮后,获取输入框中的电话号码,用作查询
		bt_query.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String phone = et_phone.getText().toString().trim();
				if(!TextUtils.isEmpty(phone)){
					//开启一个子线程用作查询
					query(phone);
				}else{
					//通过动画的工具类,加载在xml中定义的抖动动画 sin抖动
					Animation shake = AnimationUtils.loadAnimation(QueryAddresssActivity.this, R.anim.shake);
					//数学函数	y = 2x+2;
//					shake.setInterpolator(new Interpolator() {
//						@Override
//						public float getInterpolation(float input) {
//							//编写一个数学中的函数,
//							return 0;
//						}
//					});
					et_phone.startAnimation(shake);
					
					//抖动效果
					Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
					//让其抖动多久时长,按摩器
//					vibrator.vibrate(5000);
					//有规律的抖动
					vibrator.vibrate(new long[]{1000,5000,1000,5000}, -1);
				}
			}
		});
		
		//在输入文字以后,就自动去做查询,监听文字的输入过程
		et_phone.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				//在文本改变时
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				//在文本改变前
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				//在文本改变以后调用方法
				String phone = et_phone.getText().toString().trim();
				query(phone);
			}
		});
	}

	/**
	 * @param phone	查询电话号码
	 * 开启子线程查询电话号码归属地
	 */
	protected void query(final String phone) {
		new Thread(){
			public void run() {
				mQueryAddress = AddressDao.queryAddress(phone);
				//通过消息去告知主线程,可以去显示查询结果
				mHandler.sendEmptyMessage(0);
			};
		}.start();
	}
}	
